<?php


namespace App\AppContext;


use App\Facade\AppContext;
use App\Models\Log\Login;
use App\Models\System\Group;
use App\Repositories\System\RoleRepository;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Session;

/**
 * 后台管理
 * Class Admin
 * @package App\AppContext
 */
class Admin extends Base
{
    public function __construct()
    {
        $host = request()->getHost();
        if ($host == '') {
            $this->appId = 3;
        }
        $this->appId = 1;
    }

    const ADMIN_APP_CONTEXT = 'admin_app_context';

    function appId(): int
    {
        return $this->appId;
    }

    function appName(): string
    {
        return "咨询预约";
    }

    function all(): array
    {
        return Session::get(self::ADMIN_APP_CONTEXT, []);
    }

    function save(array $value): void
    {
        Session::put(self::ADMIN_APP_CONTEXT, $value);
    }

    function getToken(): string
    {
        return Session::getId();
    }

    function destroy(): void
    {
        Session::flush();
    }

    function login($userInfo)
    {
        Login::create([
            'user_id' => $userInfo['id'],
            'app_id' => $this->appId(),
            'ip' => request()->ip(),
            'token' => $this->getToken()
        ]);
        $rep = new RoleRepository();
        $permissions = $rep->permissions($userInfo['role_id'], $userInfo['group_code']);
        $permissionMap = [];
        foreach ($permissions as $permission) {
            $permissionMap[strtolower($permission)] = true;
        }
        $this->set('permissions', $permissionMap);
        list($menus, $map) = $this->_menus();
        $this->set('menus', $menus);
        $this->set('menusMap', $map);
        parent::login($userInfo);
    }

    public function menus()
    {
        $menuKey = 'admin_menus';
        $menus = __($menuKey);
        $map = [];
        $menus = $this->__menus($menus, $map, []);
        return $menus;
    }

    private function _menus()
    {
        $menuKey = 'admin_menus';
        $menus = __($menuKey);
        $map = [];
        $menus = $this->__menus($menus, $map, []);
        return [$menus, $map];
    }

    private function __menus($menus, &$map, $path)
    {
        $_menus = [];
        foreach ($menus as $key => $menu) {
            $_path = $path;
            $_path[] = $menu;
            if (isset($menu['child'])) {
                $child = $this->__menus($menu['child'], $map, $_path);
                if (count($child)) {
                    $menu['child'] = $child;
                    $_menus[] = $menu;
                }
            } else {
                if (!empty($menu['code'])) {
                    $codes = explode('@', $menu['code']);
                    if (count($codes) == 1) {
                        $codes[1] = 'index';
                    }
                    if (!AppContext::can($codes[1], $codes[0])) {
                        continue;
                    }
                }
                if (!isset($menu['url'])) {
                    $menu['url'] = $menu['code'];
                }
                $menu['icon'] = Arr::get($menu, 'icon', '');
                $_menus[] = $menu;
                $map[$menu['url']] = $_path;
            }
        }
        return $_menus;
    }

    private $appId = 1;
}
